package cn.edu.dlut.port.isp.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.dlut.smartport.utils.DBUtil;

@Controller
@ResponseBody
//@RequestMapping("/user")
public class UserLoginController {
	DBUtil db;
	// 成功，用户端
	ModelAndView mav = new ModelAndView("teach/teach1");
	// 成功，管理端
	ModelAndView mav2 = new ModelAndView("manager/managestu");
	// 失败
	ModelAndView mav3 = new ModelAndView("login");
	//登录
	@PostMapping("/login")
	public ModelAndView userLogin(String username, String password, int ident,
			HttpSession session) {
		db = new DBUtil();
		db.init();
		// 学生登陆
		if (ident == 0) {
			String studentSQL = "SELECT * from students where sname='"
					+ username + "'and password='" + password + "'";
			System.out.println(studentSQL);
			List list = db.getList(studentSQL);
			// 判断查询结果，不为空则登陆成功
			if (list.size() != 0) {
				// 获取id，存入session
				String id = (String) ((Map) list.get(0)).get("sid");
				System.out.println(id);
				session.setAttribute("id", id);
				return mav;
			} else {
				System.out.println("账号或密码错误");
				return mav3;
			}
			
			// 教师登录
		} else {
			// 判断查询结果，不为空则登陆成功
			String teacherSQL = "SELECT * from teachers where tname='"
					+ username + "'and password='" + password + "'";
			System.out.println(teacherSQL);
			List list = db.getList(teacherSQL);
			System.out.println(list);
			if (list.size() != 0) {
				// 获取教师id，存入session
				String id = (String) ((Map) list.get(0)).get("tid");
				session.setAttribute("id", id);
				Object sessionid = session.getAttribute("id");
				// 跳转路径mav2（24行）
				return mav2;
			} else {
				System.out.println("账号或密码错误");
				return mav3;
			}
		}
	}
	//注册
	@PostMapping("/register")
	public ModelAndView userRegister(String username, String password,
			String email, HttpSession session) {
		// 成功
		db = new DBUtil();
		db.init();
		// 向user表注册
		String userSQL = "INSERT INTO user(name,password,email)VALUES('"
				+ username + "','" + password + "','" + email + "')";
		int userresult = db.update(userSQL);
		// student表注册
		String studentsSQL = "INSERT INTO students(sname,password)VALUES('"
				+ username + "','" + password + "')";
		int studentsresult = db.update(studentsSQL);

		// 两表新增成功，继续
		if (userresult != 0 && studentsresult != 0) {
			// 查找user表对应的id，存入session
			String SQL2 = "SELECT * from students where sname='" + username
					+ "'and password='" + password + "'";
			List list2 = db.getList(SQL2);
			String id = (String) ((Map) list2.get(0)).get("sid");
			session.setAttribute("id", id);
			return mav;
		} else {
			return mav2;
		}
	}
}
